﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class CommonFunction {

    //通用场景切换
    public static void SceneSwitch(GameScene fromScene, GameScene toScene) {
        GlobalData.sceneLoadingAsync = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync((int)toScene, UnityEngine.SceneManagement.LoadSceneMode.Additive);
        //UnityEngine.SceneManagement.SceneManager.SetActiveScene(UnityEngine.SceneManagement.SceneManager.GetSceneByBuildIndex((int)toScene));
        UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync((int)fromScene);
        GlobalData.nowGameScene = toScene;
    }

    public static void SceneLoad(GameScene scene) {
        GlobalData.sceneLoadingAsync = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync((int)scene, UnityEngine.SceneManagement.LoadSceneMode.Additive);
        //UnityEngine.SceneManagement.SceneManager.SetActiveScene(UnityEngine.SceneManagement.SceneManager.GetSceneByBuildIndex((int)scene));
    }

    //平滑移动
    public static void SmoothMove(Transform mover, Vector3 startPos, Vector3 endPos, float nowTime, float allTime, bool check = true) {
        if (check == true) {
            mover.transform.position = Mathf.Clamp01(nowTime / allTime) * (endPos - startPos) + startPos;
        }
        else {
            mover.transform.position = (nowTime / allTime) * (endPos - startPos) + startPos;
        }
    }

    public static void SendErrorMsg(string str, _TriggerEventManager _TriggerEventManager) {
        _TriggerEventManager.TriggerEvent("ErrorMsg", str);
    }

    //通用退出游戏
    public static void Quit() {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#endif
        Application.Quit();
    }

    public static void OpenTips(RectTransform mainWindow, RectTransform tipsWindow, Camera camera, float offset) {

        tipsWindow.anchorMax = Vector2.zero;
        tipsWindow.anchorMin = Vector2.zero;

        Rect mainRect = mainWindow.rect;
        Rect tipsRect = tipsWindow.rect;
        mainRect.x = mainWindow.transform.position.x - mainWindow.pivot.x * mainRect.width;
        mainRect.y = mainWindow.transform.position.y - mainWindow.pivot.y * mainRect.height;
        //mainRect.x = camera.WorldToScreenPoint(mainWindow.transform.position).x - mainWindow.pivot.x * mainRect.width;
        //mainRect.y = camera.WorldToScreenPoint(mainWindow.transform.position).y - mainWindow.pivot.y * mainRect.height;
        Vector2 newPosition = new Vector2();

        //计算最近边
        Direction nestLine = Direction.Left;
        float leftDistance = mainRect.x + mainRect.width / 2;
        float rightDistance = Screen.width - mainRect.x - mainRect.width / 2;
        float topDistance = Screen.height - mainRect.y - mainRect.height / 2;
        float bottomDistance = mainRect.y + mainRect.height / 2;
        float minDistance = Mathf.Min(leftDistance, rightDistance, topDistance, bottomDistance);
        if (minDistance == leftDistance) nestLine = Direction.Left;
        else if (minDistance == rightDistance) nestLine = Direction.Right;
        else if (minDistance == topDistance) nestLine = Direction.Up;
        else nestLine = Direction.Down;

        //顶部20%、底部20%且更靠近顶部或者底部的mainwindow，tips优先向竖直方向展开
        if ((nestLine == Direction.Up && topDistance < Screen.height * 0.2f) ||
            (nestLine == Direction.Down && bottomDistance < Screen.height * 0.2f)) {

            //探测竖直方向
            if (mainRect.y > tipsRect.height) {//向下
                newPosition.y = mainRect.y - tipsRect.height - offset;
            }
            else {
                newPosition.y = mainRect.y + mainRect.height + offset;
            }

            //适应水平方向
            if (mainRect.x + tipsRect.width < Screen.width) {
                newPosition.x = mainRect.x;
            }
            else {
                newPosition.x = Screen.width - tipsRect.width;
            }
        }
        else {
            //探测水平方向
            if (mainRect.x + tipsRect.width + mainRect.width < Screen.width) {
                newPosition.x = mainRect.x + mainRect.width + offset;
            }
            else {
                newPosition.x = mainRect.x - tipsRect.width - offset;
            }

            //适应竖直方向
            if (mainRect.y + mainRect.height - tipsRect.height > 0) {
                newPosition.y = mainRect.y - tipsRect.height + mainRect.height;
            }
            else {
                newPosition.y = 2;
            }
        }
        tipsWindow.transform.localPosition = newPosition - new Vector2(Screen.width / 2, Screen.height / 2);

        tipsWindow.gameObject.SetActive(true);
    }

}
